package com.oa.srms.apps.actions.goods;

import java.awt.Font;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;
import org.springframework.beans.factory.annotation.Autowired;

import com.oa.srms.apps.actions.BasicAction;
import com.oa.srms.apps.business.goods.IGoodInfoService;
import com.oa.srms.apps.constants.IDEDStates;
import com.oa.srms.apps.entity.goods.GoodInfoValue;

@SuppressWarnings("serial")
public class GoodInfoAction extends BasicAction {
	
	@Autowired
	private IGoodInfoService goodInfoService;
	
	private List<GoodInfoValue> goodInfoList;
	
	private String goodId;
	private String goodIds;
	private String goodNo;
	private String goodName;
	private String goodNum;
	private String states;
	private String str;
	private String imageUrl;
	
	public String toQuery() throws Exception{
		Integer states = null;
		if(this.states != null && !"".equals(this.states)){
			states = Integer.parseInt(this.states);
		}
		String goodName = null;
		if(this.goodName != null && !"".equals(this.goodName)){
			goodName = this.goodName;
		}
		totalpage = goodInfoService.getTotalPageNumber(goodName, states, rows);
		count = goodInfoService.getTotalNumber();
		goodInfoList = goodInfoService.queryGoodInfo(goodName, states, rows, currpage);

		return "toQueryPage";
	}
	
	public String addOrModify() throws Exception{
		try{
			if(goodId == null || "".equals(goodId)){
				goodInfoService.addGoodInfo(goodNo, goodName, Integer.parseInt(goodNum));
			}else{
				goodInfoService.modifyGoodInfo(Integer.parseInt(goodId), goodNo, goodName,Integer.parseInt(goodNum));
			}
		}catch(Exception e){
			str = "-1";
		}
		str = "1";
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().write(str);
		return null;
	}
	
	public String delete() throws Exception{
		if(goodId != null)
			goodInfoService.deleteGoodInfo(Integer.parseInt(goodId));
		if(goodIds != null){
			String[] goodArr = goodIds.split(",");
			for(String goodItem : goodArr){
				goodInfoService.deleteGoodInfo(Integer.parseInt(goodItem));
			}
		}
		return "toQuery";
	}
	
	public String toEnable() throws Exception{
		goodInfoService.toEnable(Integer.parseInt(goodId));
		return "toQuery";
	}
	
	public String toDisable() throws Exception{
		goodInfoService.toDisable(Integer.parseInt(goodId));
		return "toQuery";
	}
	
	public String queryGoodInfoByImage() throws Exception{
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		List<GoodInfoValue> goodInfoList = goodInfoService.queryGoodInfo(IDEDStates.ENABLE);
		for(GoodInfoValue goodInfo:goodInfoList){
			dataset.addValue(goodInfo.getGoodNum(), "剩余量", goodInfo.getGoodName());
		}
		
		JFreeChart chart = ChartFactory.createBarChart3D("物品库存剩余量柱状图分析", "物品名称", "剩余量", dataset, PlotOrientation.VERTICAL, true, false, false);
		
		CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象
		BarRenderer3D renderer = (BarRenderer3D) plot.getRenderer();
		renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());//显示每个柱的数值 
		renderer.setBaseItemLabelsVisible(true); 
		//注意：此句很关键，若无此句，那数字的显示会被覆盖，给人数字没有显示出来的问题 
		renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition( 
		ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER)); 
		renderer.setItemLabelAnchorOffset(2D);// 设置柱形图上的文字偏离值 

		plot.setRenderer(renderer);

		CategoryAxis domainAxis=plot.getDomainAxis();
		//水平底部列表
		domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14));
		//水平底部标题
		domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));
		//垂直标题
	    ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状
	    rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));

	    chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
	    chart.getTitle().setFont(new Font("黑体", Font.BOLD, 20));
		
		ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
		String filename = ServletUtilities.saveChartAsPNG(chart, 1000, 370, info, httpSession);
		imageUrl = request.getContextPath()+"/servlet/DisplayChart?filename="+filename;
		return "toShowImage";
	}
	
	// Getter and Setter
	public String getGoodNo() {
		return goodNo;
	}

	public void setGoodNo(String goodNo) {
		this.goodNo = goodNo;
	}

	public String getGoodName() {
		return goodName;
	}

	public void setGoodName(String goodName) {
		this.goodName = goodName;
	}

	public String getGoodNum() {
		return goodNum;
	}

	public void setGoodNum(String goodNum) {
		this.goodNum = goodNum;
	}


	public List<GoodInfoValue> getGoodInfoList() {
		return goodInfoList;
	}


	public void setGoodInfoList(List<GoodInfoValue> goodInfoList) {
		this.goodInfoList = goodInfoList;
	}


	public String getStates() {
		return states;
	}


	public void setStates(String states) {
		this.states = states;
	}


	public String getGoodId() {
		return goodId;
	}


	public void setGoodId(String goodId) {
		this.goodId = goodId;
	}

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}

	public String getGoodIds() {
		return goodIds;
	}

	public void setGoodIds(String goodIds) {
		this.goodIds = goodIds;
	}

	public String getImageUrl() {
		return imageUrl;
	}

	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}
	
	

}
